<?php

namespace app\admin\controller;

use app\common\model\Node;
use app\common\model\UserType;

/**
 * 角色控制器
 * Class Role
 * @package app\admin\controller
 */
class Role extends Base
{
    /**
     * [index  角色列表]
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function getIndex()
    {
        $model = new UserType();
        $data = input('param.');
        $map = [];
        $user = $this->user;
        $map['status'] = ['egt',0];

        if(is_array($data)){
            if(!empty($data['key']))
                $map['title'] = ['like',"%" . $data['key'] . "%"];
        }

        $this->getPageAndSize($data);

        try{
            $count =$model->getRoleCountByCondition($map);
            $allpage = intval(ceil($count / $this->size));//总页面
            $lists = $model->getRoleByCondition($map, $this->from, $this->size);//列表
        }catch (\Exception $e)
        {
            return admin_json(-1, '获取失败' . $e->getMessage());
        }

        if(empty($lists))
            return admin_json(0,'数据为空');
        writelog('查看角色列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,
            'lists' => $lists,
        ]);

    }

    /**
     * [roleAdd 添加角色]
     * @return [type] [description]
     * @author [C。]
     */
    public function postAdd()
    {
        $model = new UserType();
        $user = $this->user;
        $param = input('post.');
        try{
            $id= $model->addData($param);
        }catch (\Exception $e){
            return admin_json(-1, '添加角色失败！' , '');
        }
        writelog('增加角色--'.$id,1,$user);

        return admin_json(1, '角色添加成功！');
    }

    /**
     * [roleEdit 编辑角色]
     * @return [type] [description]
     * @author [忘尘]
     */
    public function postEdit()
    {
        $model = new UserType();
        $type = input('param.type');
        $user = $this->user;
        if($type == 1)
        {
            $param = input('post.');
            try {

                $status = $model->where('id','=',$param['id'])->value('status');

                $id = $model->editData($param);

                //做管理员用户禁用操作
                if($param['status'] == 0 && $status==1)
                    $model->forbiddenRoleAdminUser($param['id']) && writelog('禁用角色'.$param['id'].'的管理员用户');

            }catch (\Exception $e) {
                return admin_json(-1, '角色修改失败！' , '');
            }
            writelog('修改角色--'.$id,1,$user);
            return admin_json(1, '角色修改成功！');
        }elseif($type == 2){
            $data = input('param.');
            $result = $this->getStatus($data);
            if($result == 1){
                writelog('启用角色--'.$data['id'],1,$user);
                return admin_json(1,'已启用角色');
            }elseif($result == 2){
                writelog('禁用角色--'.$data['id'],1,$user);
                return admin_json(2,'已禁止角色');
            }
        }

        $id = input('param.id');

        writelog('查看角色--'.$id,1,$user);

        return admin_json(1, '查看角色成功' , $model->findData($id));
    }

    /**
     * [del  删除角色]
     * @author [C。]
     * @return array
     */
    public function getDel()
    {
        $model = new UserType();
        $user = $this->user;
        $id = input('param.id');
        $res = $model->deleteFalseData(['id' => $id],['status'=>-1]);
        //做管理员禁用操作
        $model->forbiddenRoleAdminUser($id);
        writelog('删除角色'.$id,1,$user);
        return admin_json(1,'删除角色成功');
    }


    /**
     * [status  角色状态]
     * @author [C。]
     * @return array
     */
    public function getStatus($data)
    {
        $model = new UserType();

        $res = $model->changeStatus(['id' => $data['id']],$data['status']);

        if($res)
        {
            if($data['status'] == 0)
            {
                //做管理员用户禁用操作
                $model->forbiddenRoleAdminUser($data['id']);
                return 2;
            }
            else
            {
                return 1;
            }
        }
        return admin_json(-1,'操作失败');
    }



    /**
     * [giveAccess 分配权限]
     * @return [type] [description]
     * @author [C。]
     */
    public function getAccess()
    {
        $param = input('param.');
        $user = $this->user;

        $node = new Node();
        //获取现在的权限
        if('get' == $param['type']){
            $nodeStr = $node->getNode($param['id']);

            writelog('查看角色'.$param['id'].'权限',1,$user);

            return admin_json(1,'success',$nodeStr);
        }
        //分配新权限
        if('give' == $param['type']){
            $doparam = [
                'id' => $param['id'],
                'rules' => $param['rule']
            ];
            $usertype = new UserType();
            $usertype->editAccess($doparam);

            writelog('给角色'.$param['id'].'分限权限',1,$user);

            return admin_json(1,'角色权限分配成功');
        }
    }
}